﻿var aspectRatioc = 16/9;
$(function(){
	mediaFile();
	$('#uploadForm').submit(function(){ uploadSubmit(this);return false});
	cancleFile('div.uploadifyQueueItem div.cancel a','#fileQueue .uploadifyQueueItem');
	
});




function mediaFile(){
	$("#mediaFile").uploadify({
		'uploader'       : root_path+'upload/scripts/uploadify.swf',
		//'script'         : 'upload/scripts/uploadify.php',
		'script'         : root_path+'?option=uploadFileAction%26type=action%26do=uploadFile%26time='+ time(),
		'cancelImg'      : 'upload/images/cancel.png',
		'folder'         : 'tmp',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : false ,
		'queueSizeLimit' : 1,
		'fileDesc' 		 : '*.swf' , 
		'fileExt'		 : '*.swf' , 
		'sizeLimit'		 : 2 * 1000 * 1024  , //byte
		'width'			 : 120,
		'height'		 : 30,
		//'buttonText':'Select File',
		//'buttonImg': 'upload/images/submit.gif',		
		'buttonImg': 'theme/default/images/upload.png',
		onCancel: function (a, b, c, d) {
		},
		onComplete: function (evt, queueID, fileObj, response, data) {
//			alert(response.length);
//			response = replaceBom(response);
//			alert(response.length);
			$('#mediaFile'+queueID).attr('file',response);
			return false;
		},
		onSelect : function(){
			if( $('#fileQueue .uploadifyQueueItem').length >= 1 ){
				return false;
			}
		}
	});
};



function uploadSubmit(obj){
	if(checkUpload(obj)) submitUploadData(obj);
	return false;	
};

function checkUpload(obj){
	$uploadForm = $(obj);
/*	if( $('#fileQueue .uploadifyQueueItem').length == 0 ){
		$.openWin({'msg':'-_-.不能提交.您必须上传一个<span class="high">图片</span>.','status':'error','time':2000});
		return false;
	}
	if( $('#fileName').val() == '' ){
		$.openWin({'msg':'-_-.不能提交.您必须填写<span class="high">名称</span>.','status':'error','onClose':function(){$('#fileName').focus();}}); //
		return false;
	};*/
	return true;
}

function submitUploadData(obj){
	var $obj = $(obj);
	var file = '';
	$('#fileQueue .uploadifyQueueItem').each(function(){
		file += '|' + $(this).attr('file');
	});
	file = file.replace('|','');
	
	$obj.find('.submit').attr('disabled',true);
	var submitData = {
		'dataType'			: 'ajax',
		'fileName'			: $('#fileName').val(),
		'file'				: file ,
		'link'				: $('#link').val()
	};
	$.openWin({'msg':loadingBar,'canClose':false });
	$.post($obj.attr('action')+'&time='+time(),submitData,function(data){	
		if('ok'== data.status){
			$.openWin({'msg':'已经发表','time':2000});
			$('#fileName,#link').val('');
			$('#fileQueue').html('');
		}else{
			$.openWin({'time':1});
			alert(data.msg);
		}
		$obj.find('.submit').attr('disabled',false);
	},'json');
}

function cancleFile(obj,creatOrderObj){
	$(obj).live('click',function(){
		$(this).parent().parent().animate({'opacity':'hide','height':0},'slow',function(){
			$(this).remove();
			creatOrderNum(creatOrderObj);
		});
		return false;
	});
};
